package com.wisely.springbootjpa.criteria;

import com.wisely.springbootjpa.domain.User;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

public class CustomerSpecs {
    public static Specification<User> personFromBeijing() {
        return new Specification<User>() {
            /**
             * 使用root或许需要查询的属性，CriteriaQuery构造查询条件
             * 查所有来自北京的人
             */
            @Nullable
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("address"),"beijing");
            }
        };
    }
}
